package com.woorigil.android.koreaflower.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.woorigil.android.koreaflower.activity.info.InfoActivity;
import com.woorigil.android.koreaflower.activity.product.ProductListActivity;
import com.woorigil.android.koreaflower.activity.recent.LoveActivity;
import com.woorigil.android.koreaflower.activity.search.SearchActivity;
import com.woorigil.android.koreaflower.support.DisplayUtils;

public class MainTabActivity extends TabActivity {
	List<TabMenuResource> tabMenuResources = new ArrayList<TabMenuResource>() {{
		add(new TabMenuResource(R.string.tab_menu_1, R.drawable.tab_menu_1, ProductListActivity.class));
		add(new TabMenuResource(R.string.tab_menu_2, R.drawable.tab_menu_2, LoveActivity.class));
		add(new TabMenuResource(R.string.tab_menu_3, R.drawable.tab_menu_3, SearchActivity.class));
		add(new TabMenuResource(R.string.tab_menu_4, R.drawable.tab_menu_4, InfoActivity.class));
	}};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_host_activity);
		
		TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();
        
		for (int i=0; i< tabMenuResources.size(); i++) {
			View view = LayoutInflater.from(tabHost.getContext()).inflate(R.layout.tab_widget, null);
			drawTabMenuButton(view, i);
			
			TabSpec tabSpec = tabHost.newTabSpec("tab_menu_" + i).setIndicator(view);
			tabSpec.setContent(new Intent(this, tabMenuResources.get(i).getClazz()));
			tabHost.addTab(tabSpec);
		}

		tabHost.setCurrentTab(0);
		TitleBarHelper.setTabHost(tabHost);
	}
	
	private void drawTabMenuButton(View view, int position) {
		TabMenuResource tabMenuResource = tabMenuResources.get(position);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.0f);
		params.setMargins(0, DisplayUtils.pixelFromDp(this, 3), 0, 0);

		TextView tabMenu = (TextView)view.findViewById(R.id.tab_menu_text);
		tabMenu.setText(tabMenuResource.getTextResourceId());
		tabMenu.setCompoundDrawablesWithIntrinsicBounds(0, tabMenuResource.getImgResourceId(), 0, 0);
		tabMenu.setLayoutParams(params);
	}
	
	private class TabMenuResource {
		private int textResourceId;
		private int imgResourceId;
		private Class<?> clazz;
		
		public TabMenuResource(int textResourceId, int imgResourceId, Class<?> clazz) {
			this.textResourceId = textResourceId;
			this.imgResourceId = imgResourceId;
			this.clazz = clazz;
		}

		public Class<?> getClazz() {
			return clazz;
		}
		public int getTextResourceId() {
			return textResourceId;
		}
		public int getImgResourceId() {
			return imgResourceId;
		}
	}
}
